package com.fbli.demo.controller;


import com.fbli.demo.common.msg.DemoMsg;
import com.fbli.demo.common.msg.DemoUpdateMsg;
import com.fbli.demo.common.result.ResultParam;
import com.fbli.demo.common.result.ResultSend;
import com.fbli.demo.entity.Demo;
import com.fbli.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fb_li
 * @since 2021-12-07
 */
@RestController
@RequestMapping("/demo")
public class DemoController {
    @Autowired
    DemoService demoService;
    @GetMapping("/selectAll")
    public List<Demo> selectAll(){
        List<Demo> demoList = demoService.list();
        return demoList;
    }
    @PostMapping("/add")
    public ResultParam add(@RequestBody DemoMsg demoMsg) {
        String metrics = demoMsg.getMetrics();
        BigDecimal value = demoMsg.getValue();
        if (metrics.length()<=0 || metrics==null) {
            return ResultSend.fail("metrics不能为空");
        }
        if (value==null || value.equals("")){
            return ResultSend.fail("value不能为空");
        }
        boolean b = demoService.add(demoMsg);
        if (b){
            return ResultSend.success("添加成功");
        }else {
            return ResultSend.fail("添加失败");
        }
    }
    @DeleteMapping("/delete")
    public ResultParam delete(@RequestBody Long[] ids){
        List<Long> list = Arrays.asList(ids);
        boolean b = demoService.removeByIds(list);
        if (b){
            return ResultSend.success("删除成功");
        }else {
            return ResultSend.fail("删除失败");
        }
    }
    @PutMapping("/edit")
    public ResultParam edit(@RequestBody DemoUpdateMsg demoMsg){
        String metrics = demoMsg.getMetrics();
        BigDecimal value = demoMsg.getValue();
        Integer id = demoMsg.getId();
        if (metrics.length()<=0 || metrics==null) {
            return ResultSend.fail("metrics不能为空");
        }
        if (value==null || value.equals("")){
            return ResultSend.fail("value不能为空");
        }
        if (id==null || id.equals("")){
            return ResultSend.fail("id不能为空");
        }
        boolean b = demoService.insert(demoMsg);
        if (b){
            return ResultSend.success("修改成功");
        }else {
            return ResultSend.fail("修改失败");
        }
    }
}

